#!/bin/bash
ASCEND_SECLOG="/var/log/ascend_seclog"
logFile="${ASCEND_SECLOG}/ascend_install.log"
installInfo="/etc/ascend_install.info"
SHELL_DIR=$(cd "$(dirname "$0")" || exit; pwd)
COMMON_SHELL="$SHELL_DIR/common.sh"
# load common.sh, get install.info
source "${COMMON_SHELL}"
# read Driver_Install_Path_Param from installInfo
Driver_Install_Path_Param=$(getInstallParam "Driver_Install_Path_Param" "${installInfo}")
targetdir="${Driver_Install_Path_Param}"
targetdir="${targetdir%*/}"

log() {
    cur_date=`date +"%Y-%m-%d %H:%M:%S"`
    user_id=`id | awk '{printf $1}'`
    echo "[Driver] [$cur_date] [$user_id] "$1 >> $logFile
    return 0
}

# $1 : module_name
dkms_remove() {
    dkms status | grep $1 >/dev/null
    if [ $? -eq 0 ];then
        tmp_version=$(dkms status | grep $1 | grep $(uname -r) | awk -F [/' '] '{print $2}' | awk -F [,:] '{print $1}')
        if [ "$tmp_version"x = ""x ];then
            tmp_version=$(dkms status | grep $1 | grep "added" | awk -F [/' '] '{print $2}' | awk -F [,:] '{print $1}')
            if [ "$tmp_version"x = ""x ];then
                tmp_version=$(dkms status | grep $1 | grep -n 1 | awk -F [/' '] '{print $2}' | awk -F [,:] '{print $1}')
            fi
        fi

        if [ "$tmp_version"x = ""x ];then
            log "[WARNING]dkms version get faild"
            log "[WARNING]dkms status : "
            dkms status | grep $1 >>$logFile 2>&1
            return
        fi

        log "[INFO]davinci hiai kernel driver is installed($1-$tmp_version), delete and reinstall"

        dkms uninstall -m $1 -v $tmp_version --no-initrd >>$logFile 2>&1
        dkms remove -m $1 -v $tmp_version --all --no-initrd >>$logFile 2>&1
        log "[INFO]dkms remove success"

        tmp_src_dir=/usr/src/$1-$tmp_version
        if [ -d $tmp_src_dir ];then
            rm -rf $tmp_src_dir
            log "[INFO]rm -rf $tmp_src_dir success"
        fi
    fi

    return 0
}

dkms_uninstall_all() {
    sources="$targetdir"/driver/kernel
    if [ -d "$sources" ];then
        dpkg-query -s dkms 2>/dev/null | grep "Status: install ok installed" >/dev/null 2>&1
        if [ $? -ne 0 ];then
            rpm -qa 2>/dev/null | grep dkms >/dev/null 2>&1
            if [ $? -ne 0 ];then
                log "[INFO]no dkms, clear success"
                return 0
            fi
        fi

        dkms status | grep "davinci_" >& /dev/null || { log "[INFO]the drive module is not in the dkms project." && return 0; }

        module_name=$(cat "$sources"/dkms.conf | grep PACKAGE_NAME | awk -F '"' '{print $2}')
        module_version=$(cat "$sources"/dkms.conf | grep PACKAGE_VERSION | awk -F '"' '{print $2}')

        dkms_remove $module_name

        src_dir=/usr/src/$module_name-$module_version
        if [ -d $src_dir ];then
            rm -rf $src_dir
            log "[INFO]rm -rf $src_dir success"
        fi

        log "[INFO]clear dkms success"
    fi

    return 0
}

# start

dkms_uninstall_all

exit 0
